package net.bingosoft.common.struts.tags;

import java.util.Collection;

import javax.servlet.jsp.JspException;

import org.apache.struts.taglib.TagUtils;
import org.apache.struts.taglib.html.CheckboxTag;

/**
 * 2007-12-24 上午10:43:21
 * Author:yangdongyu(yangdy@bingosoft.net)
 * ReadMe:struts 的checkbox 框扩展
 * ChangeLog:
 */
public class StrutsCheckboxTag  extends CheckboxTag{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    /**
     * Determines if the checkbox should be checked.
     * @return true if checked="checked" should be rendered.
     * @throws JspException
     * @since Struts 1.2
     */
	protected boolean isChecked() throws JspException {
		Object result =
			TagUtils.getInstance().lookup(pageContext, name, property, null);

		if (result == null) {
			result = "";
		}
        if(result instanceof Collection){
        	return ((Collection)result).contains(this.value);
        	
        }else if(result instanceof Object[]){
        	for(Object obj : (Object[]) result){
        		if(obj.equals(this.value)) return true;
        	}
        }
		result = result.toString();
		String checked = (String) result;
		return (
			checked.equalsIgnoreCase(this.value)
				|| checked.equalsIgnoreCase("true")
				|| checked.equalsIgnoreCase("yes")
				|| checked.equalsIgnoreCase("on"));

	}

}
